home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15662 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  136 lines

  1. Path: howland.reston.ans.net!usc!usenet
  2. From: Zahir Yilmaz Alpaslan <alpaslan>
  3. Newsgroups: comp.lang.c
  4. Subject: beginner needs help about langtons's ant
  5. Date: 20 Apr 1996 23:27:14 GMT
  6. Organization: University of Southern California
  7. Sender: alpaslan@sal-sun36.usc.edu
  8. Message-ID: <4lbrsi$4o@usc.edu>
  9. NNTP-Posting-Host: sal-sun36.usc.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.12 (X11; I; SunOS 4.1.4-PL sun4m)
  14. X-URL: news:comp.lang.c
  15.  
  16. I am trying to implement langton's ant in c.
  17. This ant moves rigth if it's currently in a white grid and moves left if it's
  18. currently in a black grid , and changes grid color before moving to the other
  19. one. It should begin with all black grids or all empty(white) grids, to it's
  20. movement. And it should move at least 10800 times to make a highway patern.
  21.  
  22.  
  23. When I tried to run this program in unix environment it gave a segmentation
  24. fault, where did I go wrong?
  25.  
  26.  
  27.  
  28. #include<stdio.h>
  29.         char a[100][100];
  30.  
  31.     int             c;
  32.     void            grid(char);
  33.     void            regular(int, int);
  34.     void            castle(void);
  35.     int             w(int);
  36.     int             e(int);
  37.     int             s(int);
  38.     int             n(int);
  39.     void            antd(char);
  40.  
  41.     main()
  42.     {
  43.         int             e,i, j;
  44.         
  45.  
  46.                 i = 50;
  47.         j = 50;
  48.                 printf("\n\n%s\n\n\n\n",
  49.                 "Hi, do you want to begin with empty grid or full one\n"
  50.                 "for empty one enter nothing for the other enter * ");
  51.         scanf("%c", &e);
  52.                 grid(e);
  53.                 while (c <= 10800)
  54.             regular(i,j);
  55.     }
  56.  
  57.  
  58. void grid(char e)
  59. {
  60.  
  61. int i,j;
  62. for(i=0;i<=100;i++);
  63. for(j=0;j<=100;j++);
  64. a[i][j]=e;} 
  65.  
  66.  
  67.            void            regular(int i, int j)
  68.     {
  69.         char            a[100][100], b;
  70.         
  71.         if (a[i][j] == NULL)
  72.         {a[i][j]='*';
  73.             e(i);
  74.             if (a[i][j] == NULL)
  75.             {a[i][j]='*';
  76.                 s(j);
  77.                 if (a[i][j] == NULL)
  78.             {a[i][j]='*';        w(i);}
  79.                 else {a[i][j]=NULL;
  80.                     e(i);}
  81.             } else {a[i][j]=NULL;
  82.                 n(j);}
  83.             if (a[i][j] == NULL){a[i][j]='*';
  84.                 e(i);}
  85.             else{ a[i][j]=NULL;
  86.                 w(i);}
  87.         } else
  88.         {
  89.             w(i);a[i][j]=NULL;
  90.             if (a[i][j] == NULL){a[i][j]='*';
  91.                 n(j);}
  92.             else {a[i][j]=NULL;
  93.                 s(j);}
  94.         }         for (j = 0; j < 100; ++j)
  95.                                 fprintf( "%c", a[i][j]);
  96.                         fprintf( "\n");
  97.  
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.     int             n(int j)
  104.     {
  105.         c = c + 1;
  106.         return (j++);
  107.     }
  108.     int             e(int i)
  109.     {
  110.         c = c + 1;
  111.         return (i++);
  112.     }
  113.     int             w(int i)
  114.     {
  115.         c = c + 1;
  116.         return (i--);
  117.     }
  118.     int             s(int j)
  119.     {
  120.         c = c + 1;
  121.         return (j--);
  122.     }
  123.  
  124. -- 
  125.  
  126. ##########################################
  127. #                                        #
  128. #  Zahir Yilmaz Alpaslan                 #
  129. #  Minister of Treasures of Cthulhu      #
  130. #  http://www-scf.usc.edu/~alpaslan      #
  131. #  University of Southern California     #   
  132. #  Department of Electrical Engineering  #
  133. #                                        #
  134. ##########################################
  135.  
  136.